package library.console;

import java.sql.SQLException;
import java.util.InputMismatchException;
import java.util.Scanner;
import library.*;


public class AdminMenu {
	Scanner scanner;
	BookController bookController;
/*	public boolean newUser(){
		System.out.print("Enter new user name: ");
		String userName = scanner.nextLine();
	//	System.out.print("Enter new password");
	//	String password = scanner.nextLine();
		System.out.print("Enter new telephone: ");
		String tel = scanner.nextLine();
		User newUser = new User(userName,"123456",tel,null);
		try {
			UserManager.create(newUser);
			System.out.printf("User %s, created.\n", newUser.getName());
			this.loginUser = UserManager.searchUser(newUser.getName());
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (Exception e){
			e.printStackTrace();
			return false;		
		}
	}*/
	public AdminMenu(Scanner sc){
		scanner = sc;
		try {
			bookController = new BookController();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Working in progress.....");
		showAdminMenu();
	}
	
	public void showAdminMenu(){
		int option;
		do{
			option = -1;
			System.out.println("============================");
			System.out.println("  Select option ");
			System.out.println("============================");
			System.out.println("  1: Create book");
			System.out.println("  2: Quit");
			System.out.println("============================");
			
			try{
				option = scanner.nextInt();
			} catch (InputMismatchException misMatchEx){
				System.out.println("Invalid option, your input is not a number.");
				option = -1;
			} finally {
				scanner.nextLine();
			}
			
			switch(option) {
				case 1:
					createNewBook();
					break;
				case 2:
					return;
				default:
					System.out.println("Invalid option.");
					break;	
			}
		} while (option != 2);
		
	}
	
	private void createNewBook() {
		System.out.println("Enter new book name: ");
		String bookName = scanner.nextLine();
		System.out.println("Enter new book ISBN: ");
		long isbn = scanner.nextLong();
		scanner.nextLine();
		Book newBook = new Book(bookName, isbn);
		try {
			bookController.createNewBook(newBook);
			System.out.println("Book " + bookName + " created.");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
